package me.imvc.generate.template;

import java.util.*;

public class ModelTemplate
{
  protected static String nl;
  public static synchronized ModelTemplate create(String lineSeparator)
  {
    nl = lineSeparator;
    ModelTemplate result = new ModelTemplate();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "package me.imvc.app.models;" + NL + "" + NL + "import java.util.ArrayList;" + NL + "import java.util.List;" + NL + "import jxl.write.Label;" + NL + "import me.imvc.app.entities.";
  protected final String TEXT_2 = ";" + NL + "import me.imvc.core.BaseModel;" + NL + "import me.imvc.lib.DateTimeTool;" + NL + "" + NL + "public class ";
  protected final String TEXT_3 = " extends BaseModel {" + NL + "" + NL + "\tprivate ";
  protected final String TEXT_4 = " ";
  protected final String TEXT_5 = ";" + NL + "" + NL + "\tpublic ";
  protected final String TEXT_6 = " get";
  protected final String TEXT_7 = "() {" + NL + "\t\treturn ";
  protected final String TEXT_8 = ";" + NL + "\t}" + NL + "" + NL + "\tpublic void set";
  protected final String TEXT_9 = "(";
  protected final String TEXT_10 = " ";
  protected final String TEXT_11 = ") {" + NL + "\t\tthis.";
  protected final String TEXT_12 = " = ";
  protected final String TEXT_13 = ";" + NL + "\t}" + NL + "" + NL + "\tpublic ";
  protected final String TEXT_14 = " get(Long id) {" + NL + "\t\tset";
  protected final String TEXT_15 = "((";
  protected final String TEXT_16 = ") get(";
  protected final String TEXT_17 = ".class, id));" + NL + "\t\treturn get";
  protected final String TEXT_18 = "();" + NL + "\t}" + NL + "\t" + NL + "" + NL + "\tpublic void searchData(){\t\t" + NL + "\t\t hdslSearch(";
  protected final String TEXT_19 = ".class,getSearch());" + NL + "\t}" + NL + "\t" + NL + "\tpublic List<Label> getExcel() {" + NL + "\t\tList<Label> labels = new ArrayList<Label>();" + NL + "\t\tint k = 0;" + NL + "\t\tint i = 0;" + NL + "\t\t";
  protected final String TEXT_20 = NL + "\t\tlabels.add(new Label(k++,i,\"";
  protected final String TEXT_21 = "\"));" + NL + "\t\t";
  protected final String TEXT_22 = "\t\t" + NL + "\t\tfor (Object object : getDataList()) {" + NL + "\t\t\t";
  protected final String TEXT_23 = " ";
  protected final String TEXT_24 = " = (";
  protected final String TEXT_25 = ") object;" + NL + "\t\t\tk = 0;" + NL + "\t\t\ti++;" + NL + "\t\t\t";
  protected final String TEXT_26 = NL + "\t\t    labels.add(new Label(k++, i, ";
  protected final String TEXT_27 = ".get";
  protected final String TEXT_28 = "()));" + NL + "\t\t    ";
  protected final String TEXT_29 = NL + "\t\t}" + NL + "\t\treturn labels;" + NL + "\t}" + NL + "" + NL + "\tpublic void save() {" + NL + "\t\t//get";
  protected final String TEXT_30 = "().setCreatedAt(DateTimeTool.currentTime());" + NL + "\t\t//get";
  protected final String TEXT_31 = "().setUpdatedAt(DateTimeTool.currentTime());" + NL + "\t\tsave(get";
  protected final String TEXT_32 = "());" + NL + "\t}" + NL + "" + NL + "\tpublic void update(Long id) {" + NL + "\t\t";
  protected final String TEXT_33 = " newNotice=get";
  protected final String TEXT_34 = "();" + NL + "\t\t";
  protected final String TEXT_35 = " loadNotice=get(id);" + NL + "\t\tgetEntityUtils().copy(getNotUpdate().split(\",\"),newNotice, loadNotice);" + NL + "\t\t//loadNotice.setUpdatedAt(DateTimeTool.currentTime());" + NL + "\t\tupdate(loadNotice);" + NL + "\t}" + NL + "\t" + NL + "\tpublic void delete(Long id) {" + NL + "\t\tif (id == 0) {" + NL + "\t\t\tfor (Long nid : getIds()) {" + NL + "\t\t\t\tdelete(get(nid));" + NL + "\t\t\t}" + NL + "\t\t}else{" + NL + "\t\t\tdelete(get(id));" + NL + "\t\t}\t\t" + NL + "\t}" + NL + "" + NL + "}";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    
Map<String,Object> dataMap=(Map<String,Object>) argument;
LinkedHashMap<String, String> entityAttributMap=(LinkedHashMap<String, String>)dataMap.get("entityAttributMap");
String entityClass=dataMap.get("entityClass").toString();
String entityName=dataMap.get("entityName").toString();
String EntityName=me.imvc.lib.StringTools.firstToUpperCase(entityName);
String modelClass=dataMap.get("modelClass").toString();
String modelName=dataMap.get("modelName").toString();
String ModelName=me.imvc.lib.StringTools.firstToUpperCase(modelName);
String controllerPackage=dataMap.get("controllerPackage").toString();
String controllerClass=dataMap.get("controllerClass").toString();
String view=dataMap.get("view").toString();

    stringBuffer.append(TEXT_1);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(modelClass);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_4);
    stringBuffer.append(entityName);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(EntityName);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(entityName);
    stringBuffer.append(TEXT_8);
    stringBuffer.append(EntityName);
    stringBuffer.append(TEXT_9);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_10);
    stringBuffer.append(entityName);
    stringBuffer.append(TEXT_11);
    stringBuffer.append(entityName);
    stringBuffer.append(TEXT_12);
    stringBuffer.append(entityName);
    stringBuffer.append(TEXT_13);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_14);
    stringBuffer.append(EntityName);
    stringBuffer.append(TEXT_15);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_16);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_17);
    stringBuffer.append(EntityName);
    stringBuffer.append(TEXT_18);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_19);
    for (String key : entityAttributMap.keySet()){
    stringBuffer.append(TEXT_20);
    stringBuffer.append(entityAttributMap.get(key));
    stringBuffer.append(TEXT_21);
    }
    stringBuffer.append(TEXT_22);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_23);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_24);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_25);
    for (String key : entityAttributMap.keySet()){
    stringBuffer.append(TEXT_26);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_27);
    stringBuffer.append(me.imvc.lib.StringTools.firstToUpperCase(key));
    stringBuffer.append(TEXT_28);
    }
    stringBuffer.append(TEXT_29);
    stringBuffer.append(EntityName);
    stringBuffer.append(TEXT_30);
    stringBuffer.append(EntityName);
    stringBuffer.append(TEXT_31);
    stringBuffer.append(EntityName);
    stringBuffer.append(TEXT_32);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_33);
    stringBuffer.append(EntityName);
    stringBuffer.append(TEXT_34);
    stringBuffer.append(entityClass);
    stringBuffer.append(TEXT_35);
    return stringBuffer.toString();
  }
}
